//
//  DownTypeCell.m
//  搜索控制器
//
//  Created by houzhen on 2017/12/15.
//  Copyright © 2017年 multway. All rights reserved.
//

#import "UIImage+Extension.h"
#import "DownTypeCell.h"
#import "TTStandardClass.h"
#import "TTStandardKey.h"

@implementation DownTypeCell

- (void)awakeFromNib {
    [super awakeFromNib];
//    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.bounces = NO;
}
-(void)setCellWithtagArray:(NSArray *)tagArray itemSize:(CGSize)size searchDic:(NSMutableDictionary  *)dic{

    self.titleLabel.text = @"  标签：";
    _tagArray = tagArray;
    [self setScrolViewWith:tagArray itemSize:size indexTag:1000 select:@selector(btnClick:) searchDic:(NSMutableDictionary  *)dic];
}
-(void)setCellWithtypeArray:(NSArray *)typeArray itemSize:(CGSize)size searchDic:(NSMutableDictionary  *)dic{
    _typeArray = typeArray;
    self.titleLabel.text = @"  分类：";

    [self setScrolViewWith:typeArray itemSize:size indexTag:2000 select:@selector(typebtnClick:) searchDic:(NSMutableDictionary  *)dic];
}
-(void)setScrolViewWith:(NSArray*)array itemSize:(CGSize)size indexTag:(long)indexTag select:(SEL)select searchDic:(NSMutableDictionary  *)dic{
    
    for (UIView *view in self.scrollView.subviews) {
        [view removeFromSuperview];
    }

    CGFloat x = 0;
    CGFloat y = 20;
    
    CGFloat w = size.width;
    CGFloat h = size.height-40;
    
    CGFloat marge = 20;
    NSMutableArray  *btnArry = [NSMutableArray array];
    
    for (int i=0;i<array.count;i++) {
        NSString *title;
        if (indexTag == 2000) {
            TTStandardClass *standardClass = array[i];
            title = standardClass.className;
        }else{
            TTStandardKey *standarKey = array[i];
            title = standarKey.keyName;
        }
            if (i == 0) {
                x = marge *0.5;
                
            }else{
                UIButton *btn = btnArry[i - 1];
                x = btn.frame.origin.x+btn.frame.size.width + marge;
            }
            
            w = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.width + 20;
           

        UIButton *seatDetail = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
        if (indexTag == 2000) {
            TTStandardClass *standardClass = array[i];
            if (i == 0 &&!dic[@"standardClassId"]) {
                seatDetail.selected = YES;
                self.typeSelectBtn = seatDetail;
                [dic setValue:@"" forKey:@"standardClassId"];
            }else{
                if ([dic[@"standardClassId"] isEqualToString:standardClass.id]) {
                    seatDetail.selected = YES;
                    self.typeSelectBtn.selected = NO;
                    self.typeSelectBtn = seatDetail;
                }
            }
        }else{
            TTStandardKey *standardKey = array[i];

            NSArray *tagarray = dic[@"standardKeysIds"];
            for (NSString *tag in tagarray) {
                if ([standardKey.id isEqualToString:tag]) {
                    seatDetail.selected = YES;
                    break;
                }
            }
           
        }
        
        [btnArry addObject:seatDetail];
        seatDetail.tag = i+indexTag;
        [seatDetail setTitle:title forState:UIControlStateNormal];
        seatDetail.titleLabel.font = [UIFont systemFontOfSize:15];
        [seatDetail setBackgroundImage:[UIImage createImageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [seatDetail setBackgroundImage:[UIImage createImageWithColor:[TTTheme defaultManger].defaultColor] forState:UIControlStateSelected];
        [seatDetail setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        seatDetail.layer.cornerRadius = 5;
        seatDetail.layer.masksToBounds = YES;
        seatDetail.layer.borderWidth = 2;
        seatDetail.layer.borderColor = [TTTheme defaultManger].defaultColor.CGColor;
        [seatDetail addTarget:self action:select forControlEvents:UIControlEventTouchUpInside];
        
        [_scrollView addSubview:seatDetail];
        if (i==array.count -1) {
            _scrollView.contentSize = CGSizeMake(seatDetail.frame.origin.x+w+marge*0.5, size.height);
        }
    }
}
-(void)btnClick:(UIButton *)button{
    button.selected = !button.selected;
    NSLog(@"%@",self.tagArray[button.tag-1000]);
    _clickTagBlock(button);

}

-(void)typebtnClick:(UIButton *)button{
    NSLog(@"%@",self.typeArray[button.tag-2000]);
    if (self.typeSelectBtn  == button) {
        return;
    }else{
        button.selected = YES;
        self.typeSelectBtn.selected = NO;
        self.typeSelectBtn = button;
        _clickTypeBlock(button);
        
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
